package com.apes.fn.fico.expense.platformFee.process;

import com.apes.fn.fico.expense.platformFee.CommonFeeProcess;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.fn.scm.retailOrder.model.RetailOrderPayMethod;
import com.apes.fn.scm.retailOrder.repository.RetailOrderPayMethodRepository;
import com.apes.fn.scm.retailOrder.repository.RetailOrderRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 零售单生成平台费用细项单据
 * 同一单据可能满足多种条件，无法走公共方法 CommonFeeProcess 中的 createExpense 生成
 */
@Service
public class RetailOrderFeeProcess extends CommonFeeProcess {

    @Autowired
    private RetailOrderRepository retailOrderRepository;
    @Autowired
    private RetailOrderPayMethodRepository retailOrderPayMethodRepository;
    @Autowired
    private PartyRoleRepository partyRoleRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        RetailOrder retailOrder = retailOrderRepository.findOne(id);
        createExpense(retailOrder);
    }

    /**
     * 平台服务费
     *
     * @param retailOrder
     */
    private void createExpense(RetailOrder retailOrder) {
        AtomicBoolean checkProduct = new AtomicBoolean(true);
        retailOrder.getSaleItems().forEach(retailOrderItem -> {
            if (LogicalUtil.in(retailOrderItem.getProduct().getId(), "0002935068", "0002902247", "0002935072", "0003020877", "0002902250"))
                checkProduct.set(false);
        });

        if (retailOrder.getClaimType() == null) {
            //货币资金
            createExpenseHBZJ(retailOrder);
            //应收账款(外)
            createExpenseYSZKW(retailOrder);
            if (checkProduct.get()) {
                //集群引流
                createExpenseJQYL(retailOrder);
            } else {
                //e服务+
                createExpenseEFWJ(retailOrder);
            }
        }
    }

    /**
     * 货币资金平台服务费
     *
     * @param retailOrder
     */
    private void createExpenseHBZJ(RetailOrder retailOrder) {
        List<RetailOrderPayMethod> retailOrderPayMethodList = retailOrder.getPaymentMethodSet().stream()
                .filter(retailOrderPayMethod -> LogicalUtil.notIn(retailOrderPayMethod.getPaymentMethodType().getId(), "04", "10", "12", "05", "09", "003"))
                .collect(Collectors.toList());
        if (retailOrderPayMethodList.isEmpty()) return;
        Map map = MapUtil.mapper("order", retailOrder.getId(), "orderType", "RetailOrder",
                "source", retailOrder.getOrigin(), "sourceBusinessType", retailOrder.getOrderType(),
                "summary", "", "incomeTypeId", "A09", "expenseDetailsTypeId", "002051","date",retailOrder.getDateApprove(),
                "paymentAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getAmount).sum(),
                "expenseAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getPlatformManagementFee).sum(),
                "deptId", retailOrder.getDept().getId(), "companyId", retailOrder.getCompany().getId());
        createExpenseByFee(map);
    }

    /**
     * 应收账款(外)平台服务费
     *
     * @param retailOrder
     */
    private void createExpenseYSZKW(RetailOrder retailOrder) {
        List<RetailOrderPayMethod> retailOrderPayMethodList = retailOrderPayMethodRepository.findByYSZKW(retailOrder.getId());
//        List<RetailOrderPayMethod> retailOrderPayMethodList = retailOrder.getPaymentMethodSet().stream()
//                .filter(retailOrderPayMethod -> retailOrderPayMethod.getCustomer() != null)
//                .filter(retailOrderPayMethod -> "09".equals(retailOrderPayMethod.getPaymentMethodType().getId()))
//                .filter(retailOrderPayMethod -> LogicalUtil.notIn(retailOrderPayMethod.getCustomer().getId(), "C0005998", "02532856", "02533043") &&
//                        ((partyRoleRepository.findByPartyIdAndRoleId(retailOrderPayMethod.getCustomer().getParty().getId(),"Company") == null ||
//                                retailOrder.getCompany().getParty().getId() == retailOrderPayMethod.getCustomer().getParty().getId() ||
//                                (!"replacement".equals(retailOrder.getOrderType()) &&
//                                        LogicalUtil.notIn(retailOrderPayMethod.getCustomer().getId(), "K1001", "K1003"))) ||
//                                ((LogicalUtil.in(retailOrderPayMethod.getCustomer().getId(), "K1001", "K1003")) &&
//                                        "replacement".equals(retailOrder.getOrderType()) &&
//                                        (retailOrder.getCompany().getParty().getId() == retailOrderPayMethod.getCustomer().getParty().getId() ||
//                                                partyRoleRepository.findByPartyIdAndRoleId(retailOrderPayMethod.getCustomer().getParty().getId(),"Company") == null))))
//                .collect(Collectors.toList());
        if (retailOrderPayMethodList.isEmpty()) return;
        Map map = MapUtil.mapper("order", retailOrder.getId(), "orderType", "RetailOrder",
                "source", retailOrder.getOrigin(), "sourceBusinessType", retailOrder.getOrderType(),
                "summary", "", "incomeTypeId", "A09", "expenseDetailsTypeId", "002048",
                "paymentAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getAmount).sum(),"date",retailOrder.getDateApprove(),
                "expenseAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getPlatformManagementFee).sum(),
                "deptId", retailOrder.getDept().getId(), "companyId", retailOrder.getCompany().getId());
        createExpenseByFee(map);
    }

    /**
     * 集群引流平台服务费
     *
     * @param retailOrder
     */
    private void createExpenseJQYL(RetailOrder retailOrder) {
        List<RetailOrderPayMethod> retailOrderPayMethodList = retailOrderPayMethodRepository.findByJQYL(retailOrder.getId());
//        List<RetailOrderPayMethod> retailOrderPayMethodList = retailOrder.getPaymentMethodSet().stream()
//                .filter(retailOrderPayMethod -> retailOrderPayMethod.getCustomer() != null)
//                .filter(retailOrderPayMethod -> ("09".equals(retailOrderPayMethod.getPaymentMethodType().getId()) &&
//                        (LogicalUtil.in(retailOrderPayMethod.getCustomer().getId(), "K1001", "K1003", "C0005998", "02532856", "02533043") &&
//                                !"replacement".equals(retailOrder.getOrderType()) &&
//                                retailOrder.getCompany().getParty().getId() != retailOrderPayMethod.getCustomer().getParty().getId()) ||
//                        (partyRoleRepository.findByPartyIdAndRoleId(retailOrderPayMethod.getCustomer().getParty().getId(),"Company") != null &&
//                                "replacement".equals(retailOrder.getOrderType()) &&
//                                retailOrder.getCompany().getParty().getId() != retailOrderPayMethod.getCustomer().getParty().getId())))
//                .collect(Collectors.toList());
        if (retailOrderPayMethodList.isEmpty()) return;
        Map map = MapUtil.mapper("order", retailOrder.getId(), "orderType", "RetailOrder",
                "source", retailOrder.getOrigin(), "sourceBusinessType", retailOrder.getOrderType(),
                "summary", "集群引流", "incomeTypeId", "A09", "expenseDetailsTypeId", "002052","date",retailOrder.getDateApprove(),
                "paymentAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getAmount).sum(),
                "expenseAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getPlatformManagementFee).sum(),
                "deptId", retailOrder.getDept().getId(), "companyId", retailOrder.getCompany().getId());
        createExpenseByFee(map);
    }

    /**
     * e服务+平台服务费
     *
     * @param retailOrder
     */
    private void createExpenseEFWJ(RetailOrder retailOrder) {
        List<RetailOrderPayMethod> retailOrderPayMethodList = retailOrder.getPaymentMethodSet().stream()
                .filter(retailOrderPayMethod -> retailOrderPayMethod.getCustomer() != null)
                .filter(retailOrderPayMethod -> ("C0005998".equals(retailOrderPayMethod.getCustomer().getId()) ||
                        partyRoleRepository.findByPartyIdAndRoleId(retailOrderPayMethod.getCustomer().getParty().getId(),"Company") != null) &&
                        retailOrder.getCompany().getParty().getId() != retailOrderPayMethod.getCustomer().getParty().getId())
                .collect(Collectors.toList());
        if (retailOrderPayMethodList.isEmpty()) return;
        Map map = MapUtil.mapper("order", retailOrder.getId(), "orderType", "RetailOrder",
                "source", retailOrder.getOrigin(), "sourceBusinessType", retailOrder.getOrderType(),
                "summary", "e服务+", "incomeTypeId", "A09", "expenseDetailsTypeId", "002052","date",retailOrder.getDateApprove(),
                "paymentAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getAmount).sum(),
                "expenseAmount", retailOrderPayMethodList.stream().mapToDouble(RetailOrderPayMethod::getPlatformManagementFee).sum(),
                "deptId", retailOrder.getDept().getId(), "companyId", retailOrder.getCompany().getId());
        createExpenseByFee(map);
    }


    @Override
    public Map<String, Object> getExpenseModel(Map map) {
        return null;
    }

    @Override
    public boolean support(String type) {
        return "RetailOrder".equals(type);
    }
}
